﻿using System;

using PipeServer.Interface;

namespace PipeServer.Server
{
        internal class ServerInfo
        {
                /// <summary>
                /// 客户端的管理类
                /// </summary>
                private readonly Manage.ClientManage _ClientInfo = null;

                private readonly string _PublicKey = null;


                private readonly string _PipeName;
                protected Manage.ClientManage ClientInfo => this._ClientInfo;
                internal string PublicKey => this._PublicKey;
                internal int ClientCount => this._ClientInfo.ClientCount;

                public string PipeName => this._PipeName;

                internal ServerInfo (string pipeName, IAllot allot, IPipeEvent socketEvent, string publicKey)
                {
                        this._PipeName = pipeName;
                        this._PublicKey = publicKey;
                        this._ClientInfo = new Manage.ClientManage (allot, socketEvent, this);
                }
                internal ServerInfo ()
                {
                        this._ClientInfo = new Manage.ClientManage ();
                }
                internal bool CheckIsCon (Guid clientId)
                {
                        return this._ClientInfo.GetClient (clientId, out IClient client) && client.CheckIsCon ();
                }

                internal IClient[] GetClient (params Guid[] clientId)
                {
                        return this._ClientInfo.GetClient (clientId);
                }
                internal bool GetClient (Guid clientId, out IClient client, out string error)
                {
                        return this._ClientInfo.GetClient (clientId, out client, out error);
                }


                internal void CloseServer ()
                {
                        this._ClientInfo.CloseClient ();
                }

                /// <summary>
                /// 初始化服务
                /// </summary>
                /// <param name="port"></param>
                internal void InitServer ()
                {
                        this._ClientInfo.Add ();
                }

                internal void CheckIsCon ()
                {
                        if (this._ClientInfo != null)
                        {
                                this._ClientInfo.CheckIsCon ();
                        }
                }

                internal void CloseClient (Guid clientId)
                {
                        if (this._ClientInfo != null)
                        {
                                this._ClientInfo.CloseClient (clientId);
                        }
                }

                internal void Send (Guid clientId, Model.DataPage page)
                {
                        this._ClientInfo.Send (clientId, page);
                }

                internal void ClientCloseEvent (Guid clientId)
                {
                        this._ClientInfo.ClientCloseEvent (clientId);
                }


        }
}
